package cn.jingdalei.user.service.impl;

import cn.jingdalei.basic.exception.BusinessException;
import cn.jingdalei.basic.service.impl.BaseServiceImpl;
import cn.jingdalei.basic.util.MD5Utils;
import cn.jingdalei.basic.util.StrUtils;
import cn.jingdalei.user.constant.UserContants;
import cn.jingdalei.user.domain.Logininfo;
import cn.jingdalei.user.domain.User;
import cn.jingdalei.user.domain.UserDto;
import cn.jingdalei.user.mapper.LogininfoMapper;
import cn.jingdalei.user.mapper.UserMapper;
import cn.jingdalei.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    // 1. 基础的CRUD通过继承实现  先继承公共的 再实现扩展的
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    @Transactional
    public void update(User user) {
        Logininfo logininfo = user2logininfo(user);
        logininfoMapper.update(logininfo);
        userMapper.update(user);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        User user = userMapper.loadById(id);
        logininfoMapper.remove(user.getLogininfo_id());
        userMapper.remove(id);
    }

    /**
     * 用户注册
     * @param userDto
     */
    @Override
    public void register(UserDto userDto) {
        //校验
        // 校验非空
        if(StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getPwd())
                || StringUtils.isEmpty(userDto.getConfirmpwd())
                || StringUtils.isEmpty(userDto.getVerifyCode())){
            throw new BusinessException("请填入全部信息再注册");
        }
        // 校验两次密码是否一致
        if(!userDto.getConfirmpwd().equals(userDto.getPwd())){
            throw new BusinessException("两次密码不一致，请重新确认");
        }
        // 校验是否被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if(userTmp != null){
            throw new BusinessException("用户已经被注册，请直接登录");
        }
        // 校验验证码是否失效
        Object codeTmp = redisTemplate.opsForValue().get(UserContants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if(codeTmp==null){
            throw new BusinessException("验证码已失效，请重新发送");
        }
        // 校验验证码是否一致
        String code = codeTmp.toString().split(":")[0];
        if(!code.equalsIgnoreCase(userDto.getVerifyCode())){
            throw new BusinessException("验证码输入错误");
        }
        // 保存信息
        User user = userDto2user(userDto);
        Logininfo logininfo = user2logininfo(user);
        // 保存logininfo
        logininfoMapper.save(logininfo);
        // 保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2logininfo(User user) {
        //创建返回的对象
        Logininfo login = new Logininfo();
        // 按属性拷贝
        BeanUtils.copyProperties(user, login);
        login.setType(1);
        return login;
    }

    private User userDto2user(UserDto userDto) {
        // 创建返回的对象
        User user = new User();
        // 转换数据
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 获取盐值
        String stal = StrUtils.getComplexRandomString(32);
        // 拼接密码
        String stalPwd = MD5Utils.encrypByMd5(userDto.getPwd() + stal);
        user.setSalt(stal);
        user.setPassword(stalPwd);
        return user;
    }
}
